package com.jia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.entity.SysRole;
import com.jia.entity.SysRolePermission;
import com.jia.mapper.SysRolePermissionMapper;
import com.jia.service.SysRolePermissionService;
import com.jia.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杜佳佳
 * @since 2021-05-23
 */
@Service
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements SysRolePermissionService {

    @Autowired
    private SysUserRoleService sysUserRoleService;


    @Override
    public List<String> getPermissionIdsByUserId(String userId) {
        List<SysRole> sysRoles = sysUserRoleService.getRoleListByUserId(userId);

        List<String> roleIds = sysRoles.stream().map(role -> {
            return role.getId();
        }).collect(Collectors.toList());

        List<String> permissionIds = new ArrayList<>();
        if (roleIds.size() > 0){
            roleIds.forEach(roleId ->{
                QueryWrapper<SysRolePermission> wrapper = new QueryWrapper<>();
                wrapper.eq("role_id", roleId);
                List<SysRolePermission> rolePerms = baseMapper.selectList(wrapper);
                rolePerms.forEach(item ->{
                    String permissionId = item.getPermissionId();
                    if(!permissionIds.contains(permissionId)){
                        permissionIds.add(permissionId);
                    }
                });

            });
        }

        return permissionIds;

    }
}
